package com.lottery.ruok.lottrey.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LiuhecaiPrizeActivity;
import com.lottery.ruok.lottrey.ui.widget.DatePickerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/2.
 */

public class ChooseYearDialog extends DialogFragment {


    @BindView(R.id.year_pv)
    DatePickerView yearPv;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.confirm)
    TextView confirm;
    private ArrayList<String> year = new ArrayList<>();
    private String selectYear;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //自定义对话框样式
        View v = inflater.inflate(R.layout.choose_year_layout, null);

        ButterKnife.bind(this, v);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);

        yearPv.setIsLoop(false);
        int start = 1976;
        int end = Integer.parseInt(getArguments().getString("year"));
        for (int i = start; i <= end; i++) {
            year.add(i + "");
        }
        yearPv.setData(year);
        yearPv.setSelected(year.size() - 1);

        selectYear = getArguments().getString("year");
        yearPv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                selectYear = text;
            }
        });
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();

        Window window = getDialog().getWindow();
        window.setLayout(Utils.getWindowDisplay(getActivity()).widthPixels - Utils.dip2px(getActivity(), 80), Utils.getWindowDisplay(getActivity()).heightPixels / 2);
    }

    @OnClick({R.id.cancel, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                dismissAllowingStateLoss();
                break;
            case R.id.confirm:
                ((LiuhecaiPrizeActivity)(getActivity())).setYear(selectYear);
                dismissAllowingStateLoss();
                break;
        }
    }
}
